package edu.neusoft.yuejian_09.fragment;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import edu.neusoft.yuejian_09.R;
import edu.neusoft.yuejian_09.activity.ModifyUserActivity;
import edu.neusoft.yuejian_09.activity.MyActOrderListActivity;
import edu.neusoft.yuejian_09.activity.MyCollectMasterActivity;
import edu.neusoft.yuejian_09.activity.MyPostListActivity;
import edu.neusoft.yuejian_09.bean.User;
import edu.neusoft.yuejian_09.model.UserModel;
import it.sephiroth.android.library.picasso.Picasso;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MeFragment extends Fragment {
    private TextView u_name, logout, u_phone;
    private ImageView u_pic;
    private int u_id;
    private TextView modify_userinfo;
    private TextView u_order;
    private TextView u_fatie;
    private TextView user_guanzhu;
    private LinearLayout u_detail;
    private TextView user_order;
    private View view = null;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.tab05_me, container, false);
        initViews();
        initData();
        initEvents();
        return view;
    }

    private void initViews() {
        u_name = (TextView) view.findViewById(R.id.user_name);
        u_pic = (ImageView) view.findViewById(R.id.user_pic);
        u_detail = (LinearLayout) view.findViewById(R.id.user_detail);
        user_order = (TextView) view.findViewById(R.id.user_order);
        u_fatie= (TextView) view.findViewById(R.id.user_fatie);
        user_guanzhu= (TextView) view.findViewById(R.id.user_guanzhu);
       /* u_order = (TextView) view.findViewById(R.id.user_order);
        u_fatie = (TextView) view.findViewById(R.id.user_fatie);
        u_guanzhu = (TextView) view.findViewById(R.id.user_guanzhu);*/
        logout = (TextView) view.findViewById(R.id.user_quit);
        u_phone = (TextView) view.findViewById(R.id.user_phone);
    }

    public void initEvents() {
        u_detail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(MeFragment.this.getActivity(), ModifyUserActivity.class);
                startActivity(intent);
            }
        });
        user_order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(MeFragment.this.getActivity(), MyActOrderListActivity.class);
                startActivity(intent);
            }
        });

        u_fatie.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(MeFragment.this.getActivity(), MyPostListActivity.class);
                startActivity(intent);
            }
        });

        user_guanzhu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(MeFragment.this.getActivity(), MyCollectMasterActivity.class);
                startActivity(intent);
            }
        });

        logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick (View view){
                new AlertDialog.Builder(getActivity()).setTitle("系统提示")//设置对话框标题
                        .setMessage("确定要退出系统？")//设置显示的内容
                        .setPositiveButton("确定",new DialogInterface.OnClickListener() {//添加确定按钮
                            @Override
                            public void onClick(DialogInterface dialog, int which) {//确定按钮的响应事件
                                System.exit(0);
                            }
                        }).setNegativeButton("返回",new DialogInterface.OnClickListener() {//添加返回按钮
                    @Override

                    public void onClick(DialogInterface dialog, int which) {//响应事件
                        dialog.dismiss();
                    }

                }).show();//在按键响应事件中显示此对话框
            }


        });

    }

    public void initData() {
        SharedPreferences sp = getActivity().getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        u_id = sp.getInt("user_id", 0);
        // Log.d("userbg","u_id");
        UserModel userModel = new UserModel();
        Call call = userModel.getUserInfo(u_id);
        call.enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, Response<User> response) {
                User user = response.body();
                u_name.setText(user.getUser_name());
                u_phone.setText(user.getUser_phone()+" ");
//               Log.d("userbg","username");
                String upic = "http://10.0.2.2/neu1_yuejian/Public/Admin/photo/" + user.getUser_pic();
                Picasso.with(MeFragment.this.getActivity()).load(upic).into(u_pic);
            }

            @Override
            public void onFailure(Call<User> call, Throwable t) {
            }
        });
    }
}


      /*  addfeedback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(MeFragment.this.getActivity(), AddFeedbackActivity.class);
                startActivity(intent);
            }
        });


        user_order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick (View view){
                Intent intent = new Intent();
                intent.setClass(MeFragment.this.getActivity(), MyPostActivity.class);
                startActivity(intent);
            }
        });

        mydeal_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick (View view){
                Intent intent = new Intent();
                intent.setClass(MyFragment.this.getActivity(), MyTrainOrderListActivity.class);
                startActivity(intent);
            }
        });


        mycollect_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick (View view){
                Intent intent = new Intent();
                intent.setClass(MyFragment.this.getActivity(), MyCollectListActivity.class);
                startActivity(intent);
            }
        });

        logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick (View view){
                new AlertDialog.Builder(getActivity()).setTitle("系统提示")//设置对话框标题
                        .setMessage("确定要退出系统？")//设置显示的内容
                        .setPositiveButton("确定",new DialogInterface.OnClickListener() {//添加确定按钮
                            @Override
                            public void onClick(DialogInterface dialog, int which) {//确定按钮的响应事件
                                System.exit(0);
                            }
                        }).setNegativeButton("返回",new DialogInterface.OnClickListener() {//添加返回按钮
                    @Override

                    public void onClick(DialogInterface dialog, int which) {//响应事件
                        dialog.dismiss();
                    }

                }).show();//在按键响应事件中显示此对话框
            }


        });


    }*/
